import os
from flask import current_app
from sqlalchemy import desc
from applications.extensions import db
from applications.extensions.init_upload import photos,reports,tools,supplierRawYp,customerRawYp,customerFinishedYp,storeRawYp
from applications.models import Photo
from applications.schemas import PhotoOutSchema
from applications.common.curd import model_to_dicts


def get_photo(page, limit):
    photo = Photo.query.order_by(desc(Photo.create_time)).paginate(page=page, per_page=limit, error_out=False)
    count = Photo.query.count()
    data = model_to_dicts(schema=PhotoOutSchema, data=photo.items)
    return data, count


def upload_one(photo, mime):
    filename = photos.save(photo)
    '''
    #/_uploads/photos/  这个是flask_uploads.py模块中设置的专用路由
     _uploads  这个是主路由
     photos   是自己设置的注册到flask_uploads上的  UploadSet('photos', IMAGES)
    
    '''
    file_url = '/_uploads/photos/'+filename
    # file_url = photos.url(filename)
    upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    size = os.path.getsize(upload_url + '/' + filename)
    photo = Photo(name=filename, href=file_url, mime=mime, size=size)
    db.session.add(photo)
    db.session.commit()
    return file_url


def delete_photo_by_id(_id):
    photo_name = Photo.query.filter_by(id=_id).first().name
    photo = Photo.query.filter_by(id=_id).delete()
    db.session.commit()
    upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    os.remove(upload_url + '/' + photo_name)
    return photo


def upload_report_one(report, mime):
    filename = reports.save(report)
    '''
    #/_uploads/photos/  这个是flask_uploads.py模块中设置的专用路由
     _uploads  这个是主路由
     photos   是自己设置的注册到flask_uploads上的  UploadSet('photos', IMAGES)

    '''
    file_url = '/_uploads/reports/' + filename
    # file_url = photos.url(filename)
    # upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    # size = os.path.getsize(upload_url + '/' + filename)
    # photo = Photo(name=filename, href=file_url, mime=mime, size=size)
    # db.session.add(photo)
    # db.session.commit()
    return file_url,filename

def upload_tools_one(report, mime):
    filename = tools.save(report)
    '''
    #/_uploads/photos/  这个是flask_uploads.py模块中设置的专用路由
     _uploads  这个是主路由
     photos   是自己设置的注册到flask_uploads上的  UploadSet('photos', IMAGES)

    '''
    file_url = '/_uploads/tools/' + filename
    # file_url = photos.url(filename)
    # upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    # size = os.path.getsize(upload_url + '/' + filename)
    # photo = Photo(name=filename, href=file_url, mime=mime, size=size)
    # db.session.add(photo)
    # db.session.commit()
    return file_url,filename

def upload_supplier_raw_yp_one(report, mime):
    filename = supplierRawYp.save(report)
    '''
    #/_uploads/photos/  这个是flask_uploads.py模块中设置的专用路由
     _uploads  这个是主路由
     photos   是自己设置的注册到flask_uploads上的  UploadSet('photos', IMAGES)

    '''
    file_url = '/_uploads/supplierRawYp/' + filename
    # file_url = photos.url(filename)
    # upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    # size = os.path.getsize(upload_url + '/' + filename)
    # photo = Photo(name=filename, href=file_url, mime=mime, size=size)
    # db.session.add(photo)
    # db.session.commit()
    return file_url,filename

def upload_store_raw_yp_one(report, mime):
    filename = storeRawYp.save(report)
    '''
    #/_uploads/photos/  这个是flask_uploads.py模块中设置的专用路由
     _uploads  这个是主路由
     photos   是自己设置的注册到flask_uploads上的  UploadSet('photos', IMAGES)

    '''
    file_url = '/_uploads/storeRawYp/' + filename
    # file_url = photos.url(filename)
    # upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    # size = os.path.getsize(upload_url + '/' + filename)
    # photo = Photo(name=filename, href=file_url, mime=mime, size=size)
    # db.session.add(photo)
    # db.session.commit()
    return file_url,filename
def upload_customer_raw_yp_one(report, mime):
    filename = customerRawYp.save(report)
    '''
    #/_uploads/photos/  这个是flask_uploads.py模块中设置的专用路由
     _uploads  这个是主路由
     photos   是自己设置的注册到flask_uploads上的  UploadSet('photos', IMAGES)

    '''
    file_url = '/_uploads/customerRawYp/' + filename
    # file_url = photos.url(filename)
    # upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    # size = os.path.getsize(upload_url + '/' + filename)
    # photo = Photo(name=filename, href=file_url, mime=mime, size=size)
    # db.session.add(photo)
    # db.session.commit()
    return file_url,filename

def upload_customer_finished_yp_one(report, mime):
    filename = customerFinishedYp.save(report)
    '''
    #/_uploads/photos/  这个是flask_uploads.py模块中设置的专用路由
     _uploads  这个是主路由
     photos   是自己设置的注册到flask_uploads上的  UploadSet('photos', IMAGES)

    '''
    file_url = '/_uploads/customerFinishedYp/' + filename
    # file_url = photos.url(filename)
    # upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    # size = os.path.getsize(upload_url + '/' + filename)
    # photo = Photo(name=filename, href=file_url, mime=mime, size=size)
    # db.session.add(photo)
    # db.session.commit()
    return file_url,filename